package com.sky.chainOfResponsibility;


import java.util.ArrayList;

/**
 * @author : wushikai
 * <p>
 * date : 2020-08-18
 */

/*--------------责任链模式实例---------------

 * 假如规定学生请假
 *   小于或等于 2 天，班主任可以批准；
 *   小于或等于 7 天，系主任可以批准；
 *   小于或等于 10 天，院长可以批准；
 *   小于或等于 30 天，校长可以批准；
 * 其他情况不予批准；
 *
 * */

public class LeaveApproval {
}


//抽象处理者: 领导类
abstract class Leader {
    Leader( ) {
    }
    private String duty;
    int  handleDays ;

    public int getHandleDays( ) {
        return handleDays;
    }

    public void setHandleDays(int handleDays) {
        this.handleDays = handleDays;
    }

    public String getDuty( ) {
        return duty;
    }

    public void setDuty(String duty) {
        this.duty = duty;
    }

    Leader(String name) {
        this.name = name;
    }

    private String name;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    //下一个处理者 < -----------------------重点
    private Leader next;

    private Leader getNext( ) {
        return next;
    }

    void setNext(Leader next) {
        this.next = next;
    }


    //处理请求的方法    <---------------------重点
    void handleRequest(int studentLeaveDays){

        int handleDays = this.handleDays;
        if (studentLeaveDays <= handleDays) {
            System.out.println(getDuty() +  ""+ getName() + " 批准您请假" + studentLeaveDays + "天。");
        } else {
            System.out.println("请假天数太多，"+ getName() +" 不能 批准该假条！");
            //自己处理不了, 但是  如果有下一个人, 就让他去处理
            if (getNext() != null) {
                getNext().handleRequest(studentLeaveDays);
            } else {
                //没有人了, 那就无法处理了;
                System.out.println("请假天数太多: " + studentLeaveDays +"，没有一个人  可以 批准该假条！");
            }
        }

    };
}

/**
 * 具体处理者, 班主任
 */

class Adviser extends Leader {

    private String name;
    private String duty;


    @Override
    public String getName( ) {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }


    public String getDuty( ) {
        return duty;
    }

    public void setDuty(String duty) {
        this.duty = duty;
    }

    Adviser(String name   ) {

        this.name =name;
        this.duty ="班主任";
        this.handleDays = 2 ;
    }

}


/**
 * 具体处理者2：系主任类
 */

class DepartmentHead extends Leader {

    private String name;
    private String duty;

    public String getDuty( ) {
        return duty;
    }

    public void setDuty(String duty) {
        this.duty = duty;
    }
    @Override
    public String getName( ) {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }
    DepartmentHead(String name  ) {

        this.name= name;
        this.duty= "系主任";
        handleDays =  7 ;
    }


}




/**
 * 具体处理者3：院长类
 */

class Dean  extends Leader {

    String name;
    private String duty;

    @Override
    public String getName( ) {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }


    public String getDuty( ) {
        return duty;
    }

    public void setDuty(String duty) {
        this.duty = duty;
    }
    public Dean (String name   ) {
        this.name= name;
        this.duty =  "院长" ;
        this.handleDays = 15;
    }


}
/**拓展一个校长类*/
class President extends Leader{
    private String name;
    private String duty;


    public String getDuty( ) {
        return duty;
    }

    public void setDuty(String duty) {
        this.duty = duty;
    }
    @Override
    public String getName( ) {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }
    President(String name  ) {

        this.name =name;
        this.duty  = "校长" ;
        this.handleDays = 32 ;
    }


}

    class Student {


    public static void main(String[] args){

        Leader             adviser                  = new Adviser("王艳");
        Leader             departmentHead           = new DepartmentHead("梅华威");
        Leader             dean                     = new Dean("王保义");
        Leader             president                = new President("杨勇平");



        ArrayList<Leader> leaders = new ArrayList<>();
        leaders.add( adviser        );
        leaders.add( departmentHead );
        leaders.add( dean           );
        leaders.add( president      );


        adviser = buildNext(leaders, adviser);

        System.out.println("吴世凯请假");
        adviser.handleRequest(11);

        System.out.println("--------------------------------------------------");


        System.out.println("赵傻奎请假");
        adviser.handleRequest(30);

    }


    /**
     * 当然需要一个排序机制来处理这些责任链条对象 ;
     *
     * */
    private static  Leader buildNext(ArrayList<Leader> leaders, Leader leader) {
        Leader next  = leaders.get(0);
        leader.setNext(next );
        leaders.remove(0);

        if (leaders.size()>0){
            buildNext(leaders, next  ) ;
        }
        return leader ;

    }

}

